package ferp.core.ai.tree2;

import ferp.core.card.Card;

/**
 * User: igorgok
 * Date: 4/10/12 12:49 PM
 */
final class Move extends LinkedList.Entry<Move>
{
  public static final Pool<Move> pool = new Pool<Move>()
  {
    @Override
    protected LinkedList<Move> initialize() {return new LinkedList<Move>(Move.class);}
    @Override
    protected Move create() {return new Move();}
  };

  // 8 bit compressed cards for the whole round
  public byte c0;
  public byte c1;
  public byte c2;
  // position the card above leads to
  public Node node;

  public static Move create(Card c0, Card c1, Card c2, Node node)
  {
    Move m = pool.get();

    m.c0 = c0.compressed;
    m.c1 = c1.compressed;
    m.c2 = c2.compressed;
    m.node = node;

    return m;
  }

  // get card according to the hand
  public final byte card(int hand)
  {
    switch (hand)
    {
      case 0: return c0;
      case 1: return c1;
      case 2: return c2;
    }

    return 0;
  }

  // put object back to the pool
  public final void destroy()
  {
    --node.parents;

    // debug code
/*
    if (node.parents < 0)
      throw new AssertionError("parents < 0");
*/

    // return to the pool
    pool.put(this);
  }

  @Override
  public String toString()
  {
    return Card.card(c0).toString() + ' ' + Card.card(c1) + ' ' + Card.card(c2);
  }
}
